package com.tairanchina.md.meeting.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.tairanchina.md.meeting.constants.MeetingConstants;

public class DateUtil {
	
	/**
	 * 获取当天日期
	 * @param format
	 * @return
	 */
	public static String getToDayDate(String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(new Date());
	}
	/**
	 * 获取前一天的开始时间
	 * @return
	 */
	public static Long getToDayStartTime(){  
        Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0); 
        return todayStart.getTime().getTime();  
    }
	
	/**
	 * 获取某一天的日期
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getSomedayDate(String format,int days){
		Calendar   cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE,days);
		return new SimpleDateFormat(format).format(cal.getTime());
	}
	
	
	/**
	 * 把字符串表示的日期根据格式转换成date对象
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date getString2Date(String dateStr,String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}
		
	}
	
	
	/**
	 * 获取指定天的前面的几天或者后面的几天
	 * @param specifiedDay
	 * @param formmat
	 * @param days
	 * @return
	 */
	public static String getSpecifiedDayBefore(String specifiedDay,String formmat,int days){ 
		Calendar c = Calendar.getInstance(); 
		Date date=null; 
		try { 
		date = new SimpleDateFormat(formmat).parse(specifiedDay); 
		} catch (ParseException e) { 
		e.printStackTrace(); 
		} 
		c.setTime(date); 
		int day=c.get(Calendar.DATE); 
		c.set(Calendar.DATE,day-days); 
		String dayBefore=new SimpleDateFormat(formmat).format(c.getTime()); 
		return dayBefore; 
		} 
	
	
	
	public static void main(String[] args){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd:HHmmss");
		System.out.println(dateFormat.format(getToDayStartTime()));
		System.out.println(getToDayStartTime());
		System.out.println(getString2Date("20161129", "yyyyMMdd"));
		System.out.println(getSpecifiedDayBefore("2016-12-15 00:00:00", MeetingConstants.AWARD_DATE_AND_TIME_FORMART, -1));
		List list = new ArrayList(1000000);
		for(int i=0;i<1000000;i++){
			list.add(i);
		}
		long start = System.currentTimeMillis();
		list.get(0);
		System.out.println("获取第0个所需时间"+(System.currentTimeMillis()-start));
		
		start = System.currentTimeMillis();
		list.get(100);
		System.out.println("获取第100个所需时间"+(System.currentTimeMillis()-start));
		
		start = System.currentTimeMillis();
		list.get(1000);
		System.out.println("获取第1000个所需时间"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		list.get(9999);
		System.out.println("获取第9999个所需时间"+(System.currentTimeMillis()-start));
		
		start = System.currentTimeMillis();
		list.get(19999);
		System.out.println("获取第19999个所需时间"+(System.currentTimeMillis()-start));
		
		
		start = System.currentTimeMillis();
		list.get(199990);
		System.out.println("获取第199990个所需时间"+(System.currentTimeMillis()-start));
		
		start = System.currentTimeMillis();
		list.get(599990);
		System.out.println("获取第599990个所需时间"+(System.currentTimeMillis()-start));
		
		start = System.currentTimeMillis();
		list.get(999990);
		System.out.println("获取第999990个所需时间"+(System.currentTimeMillis()-start));
		
	}
	
	
	
}
